package com.shuoyue.ycgk.ui.questionbank;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.UploadEstimateSucInfo;
import com.shuoyue.ycgk.entity.UserAnswerPager;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.base.ApiSubscriberNoView;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import io.reactivex.Observable;

public interface UploadAnswerContract {
    interface View extends BaseView {

        void uploadSuc(boolean b);

        void uploadEstimatteSuc(boolean b, UploadEstimateSucInfo uploadEstimateSucInfo, int submit);

        void uploadFail(boolean b, int submit);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();


        /**
         * @param userAnser
         * @param type      1：专项训练,2,模考 3 真题 4估分  5 自由组卷用专项
         * @param isFinish
         */
        public void uploadAnswer(UserAnswerPager userAnser, int type, boolean isFinish) {
            if (userAnser == null) {
                return;
            }
            Observable<BaseResult<String>> observable = null;
            Observable<BaseResult<UploadEstimateSucInfo>> observable2 = null;
            switch (type) {
                case 1:
                    observable = model.uploadSpecialAnswer(userAnser);
                    break;
                case 2:
                    observable = model.uploadMokAnswer(userAnser);
                    break;
                case 3:
                    observable = model.uploadSetAnswer(userAnser);
                    break;
                case 4:
                    observable2 = model.uploadEstimateAnswer(userAnser);
                    break;
                case 5:
                    observable = model.uploadSpecialAnswer(userAnser);
                    break;
            }

            if (observable != null) {
                apply(observable).subscribe(new ApiSubscriber<Optional<String>>(mView, this,true) {
                    @Override
                    public void onNext(Optional<String> optional) {
                        super.onNext(optional);
                        mView.uploadSuc(isFinish);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.uploadFail(isFinish, userAnser.getIsSubmit());
                    }
                });
            } else if (observable2 != null) {
                apply(observable2).subscribe(new ApiSubscriber<Optional<UploadEstimateSucInfo>>(mView, this) {
                    @Override
                    public void onNext(Optional<UploadEstimateSucInfo> optional) {
                        super.onNext(optional);
                        mView.uploadEstimatteSuc(isFinish, optional.getIncludeNull(), userAnser.getIsSubmit());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.uploadFail(isFinish, userAnser.getIsSubmit());
                    }
                });
            }
        }

        public void uploadAnswerNoView(UserAnswerPager userAnser, int type, boolean isFinish) {
            if (userAnser == null) {
                return;
            }
            Observable<BaseResult<String>> observable = null;
            Observable<BaseResult<UploadEstimateSucInfo>> observable2 = null;
            switch (type) {
                case 1:
                    observable = model.uploadSpecialAnswer(userAnser);
                    break;
                case 2:
                    observable = model.uploadMokAnswer(userAnser);
                    break;
                case 3:
                    observable = model.uploadSetAnswer(userAnser);
                    break;
                case 4:
                    observable2 = model.uploadEstimateAnswer(userAnser);
                    break;
                case 5:
                    observable = model.uploadSpecialAnswer(userAnser);
                    break;
            }

            if (observable != null) {
                apply(observable).subscribe(new ApiSubscriberNoView<Optional<String>>(this) {
                    @Override
                    public void onNext(Optional<String> optional) {
                        super.onNext(optional);
                    }
                });
            } else if (observable2 != null) {
                apply(observable2).subscribe(new ApiSubscriberNoView<Optional<UploadEstimateSucInfo>>(this) {
                    @Override
                    public void onNext(Optional<UploadEstimateSucInfo> optional) {
                        super.onNext(optional);
                    }

                });
            }
        }

    }

    class Model extends BaseModel {
        /**
         * 专项训练提交答案
         *
         * @return
         */
        Observable<BaseResult<String>> uploadSpecialAnswer(UserAnswerPager userAnser) {
            return RetrofitClient.getInstance().getApi().uploadSpecialAnswer(userAnser);
        }

        /**
         * 真题/题库/冲刺提交答案
         *
         * @return
         */
        Observable<BaseResult<String>> uploadSetAnswer(UserAnswerPager userAnser) {
            return RetrofitClient.getInstance().getApi().uploadSetAnswer(userAnser);
        }

        /**
         * 估分提交答案
         *
         * @return
         */
        Observable<BaseResult<UploadEstimateSucInfo>> uploadEstimateAnswer(UserAnswerPager userAnser) {
            return RetrofitClient.getInstance().getApi().uploadEstimateAnswerV1(userAnser);
        }

        /**
         * 模考提交答案
         *
         * @return
         */
        Observable<BaseResult<String>> uploadMokAnswer(UserAnswerPager userAnser) {
            return RetrofitClient.getInstance().getApi().uploadMockAnwser(userAnser);
        }
    }
}
